<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Realtime_data_model extends MY_Model {

	const TABLE = 'realtime_data';

	public function add($item_id, $data_value, $data_time, $data_server) {
		$data = array(
			'item_id'      => $item_id,
			'data_value'   => $data_value,
			'data_server'  => $data_server,
			'data_time' => $data_time
		);
		$this->db->insert(self::TABLE, $data);
		return $this->db->insert_id();
	}

	public function update($data_id, $data_value, $data_time) {
		$data = array(
			'data_value'  => $data_value,
			'data_time' => time(),
			'data_time'   => $data_time,
		);
		$this->db->where('data_id', $data_id);
		$this->db->update(self::TABLE, $data);
		return $this->db->insert_id();
	}

	public function get_data_list() {
		$query = $this->db->query("select * from realtime_data order by data_id desc");
		return $query->result_array();
	}

	public function get_latest_data($item_id) { 
		$this->db->limit(100);
		$this->db->order_by('data_id', "DESC");
		$query = $this->db->get_where(self::TABLE, array('item_id' => $item_id));
		return $query->result_array();
	}

	public function add_alarm($alarm_content, $alarm_time) { 
		$data = array(
			'alarm_content'      => $alarm_content,
			'alarm_time'   => $alarm_time
		);
		$this->db->insert("alarm_info", $data);
		return $this->db->insert_id();
	}

	public function check_alarm() { 
		$query = $this->db->query("select * from alarm_info where solve=0 order by alarm_id desc");
		return $query->result_array();
	}

	public function solve_alarm($alarm_id) { 
		$data = array(
			'solve'  => 1,
		);
		$this->db->where('alarm_id', $alarm_id);
		$this->db->update("alarm_info", $data);
		return $this->db->insert_id();
	}

}